import { Tab } from '@/store/tabsStatus'
/**
 * @description 处理tabs 标签页删除后的路由
 * @param {ITable[]} tables tabs 标签页数据
 * @param {string} path 当前的路由
 * @return string
 */
export const handleDeletePath = (tabs: Tab[], path: string) => {
  const index = tabs.findIndex(i => i?.path === path)
  const currentIndex = index === tabs.length - 1 ? index - 1 : index + 1
  return tabs[currentIndex]?.path
}

/**
 * @description: 用来获取激活的tabs的name
 * @param {any} routes 路由
 * @param {string} path 当前路由的path
 * @return {string[]} 当前路由的name
 */
export const handleMenuTitle = (routes: any, path: string): string[] => {
  // 返回值
  const result: string[] = []
  const mergeRouteName = (routes: any, path: string) => {
    // 返回一个数组,包含激活的tab的name
    return routes.reduce((pre: string[], route: any) => {
      // 判断是否有path,判断的路由path是否包含当前路由path,路由name
      if (route.path && path.includes(route.path) && route?.name) {
        // true push路由name
        pre.push(route?.name)
        // 如果存在children,递归实现
        if (route?.children) mergeRouteName(route?.children, path)
      }
      // 返回name数组
      return pre
    }, result)
  }
  return mergeRouteName(routes, path)
}
/**
 * @description 处理侧边菜单的展开项
 * @param {string} path 当前的路由
 * @return string[]
 */
export const getOpenKeys = (path: string) => {
  let newStr = ''
  const newArr: string[] = []
  const arr = path.split('/').map(i => '/' + i)
  for (let i = 1; i < arr.length - 1; i++) {
    newStr += arr[i]
    newArr.push(newStr)
  }
  return newArr
}
